home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Pyramid / Source / Pyramid.m < prev    next >
Text File  |  1993-09-15  |  3KB  |  148 lines

  1. /*    Pyramid.m - Pyramid model.
  2.  *    Copyright (C) 1993 Corona Design, Inc. All rights reserved.
  3.  *
  4.  *    Abstract
  5.  *        This model displays a tetrahedron with colored sides.
  6.  *
  7.  *    RCS path: 
  8.  *        $Source: /Users/pkron/Projects/voxel/Pyramid/RCS/Pyramid.m,v $
  9.  *    Modified: $Date: 93/09/15 12:35:12 $ by $Author: pkron $
  10.  *    Current State: $State: Exp $ locked by $Locker:  $
  11.  */
  12.  
  13. #import "Pyramid.h"
  14.  
  15. #import    <math.h>
  16.  
  17.  
  18.  
  19. #define    SQRT2    1.414214    
  20. #define    SQRT3    1.732051
  21. #define    SQRT6    (SQRT2*SQRT3)
  22.  
  23. @implementation Pyramid
  24.  
  25.  
  26.     // unit tetrahedron with base on X,Y plane, vertex on Z-axis
  27. static POINT    pyramid3D[4] = 
  28.     {
  29.         { -.5,  SQRT3/6,       0, 1},
  30.         {  .5,  SQRT3/6,       0, 1},
  31.         {   0, -SQRT3/3,       0, 1},
  32.         {   0,        0, SQRT6/3, 1}
  33.     };
  34.  
  35. - drawFace: (int)point1 : (int)point2 : (int)point3
  36.     {
  37.     PSmoveto( pyramid[ point1][0], pyramid[ point1][1]);
  38.     PSlineto( pyramid[ point2][0], pyramid[ point2][1]);
  39.     PSlineto( pyramid[ point3][0], pyramid[ point3][1]);
  40.     PSclosepath();
  41.     PSfill();    
  42.     return( self);
  43.     }
  44.  
  45.  
  46. - drawModel: (MATRIX)transform : (NXRect *)clip;
  47.     {
  48.     int        i;
  49.     float    maxZ;
  50.     BOOL    walked[4];
  51.     int        drawOrder[4];
  52.     NXRect    tmpRect;
  53.  
  54.                                 // map pyramid into view space        
  55.     for ( i=0; i LT 4; ++i )
  56.         {
  57.         pyramid[i][0] = pyramid3D[i][0];
  58.         pyramid[i][1] = pyramid3D[i][1];
  59.         pyramid[i][2] = pyramid3D[i][2];
  60.         pyramid[i][3] = pyramid3D[i][3];
  61.         map( transform, &pyramid[i]);
  62.         }
  63.     
  64.                                 // walk vertexes from closest to back    
  65.     walked[0] = walked[1] = walked[2] = walked[3] = NO;
  66.     for ( i=0; i LT 4; ++i)
  67.         {
  68.         int        max = 0;
  69.         
  70.         maxZ = 1E10;
  71.  
  72.         if ( !walked[0] )
  73.             maxZ = pyramid[0][2];
  74.             
  75.         if ( !walked[1] && pyramid[1][2] LT maxZ )
  76.             {
  77.             max = 1;
  78.             maxZ = pyramid[1][2];
  79.             }
  80.  
  81.         if ( !walked[2] && pyramid[2][2] LT maxZ )
  82.             {
  83.             max = 2;
  84.             maxZ = pyramid[2][2];
  85.             }
  86.  
  87.         if ( !walked[3] && pyramid[3][2] LT maxZ )
  88.             max = 3;
  89.         
  90.         walked[ max] = YES;
  91.         drawOrder[i] = max;
  92.         }
  93.  
  94.                                 // now project onto view plane
  95.     for ( i=0; i LT 4; ++i )
  96.         project( &pyramid[i]);
  97.         
  98.     for ( i=0; i LT 4; ++i )
  99.         {
  100.                                 // draw face opposite frontmost vertex        
  101.         switch( drawOrder[i] )
  102.             {
  103.             case 0:
  104.                 PSsetrgbcolor( 1, 1, 0);    // yellow
  105.                 [self drawFace: 1 : 2 : 3];
  106.                 break;
  107.                 
  108.             case 1:
  109.                 PSsetrgbcolor( 0, 0, 1);    // blue
  110.                 [self drawFace: 0 : 2 : 3];
  111.                 break;
  112.                 
  113.             case 2:
  114.                 PSsetrgbcolor( 0, 1, 0);    // green
  115.                 [self drawFace: 0 : 1 : 3];
  116.                 break;
  117.                 
  118.             case 3:
  119.                 PSsetrgbcolor( 1, 0, 0);    // red
  120.                 [self drawFace: 0 : 1 : 2];
  121.                 break;
  122.             }
  123.         }
  124.  
  125.                                 // compute new bounding rectangle
  126.     NXSetRect( clip, pyramid[0][0] - .1, pyramid[0][1] - .1, .2, .2);
  127.     NXSetRect( &tmpRect, pyramid[1][0] - .1, pyramid[1][1] - .1, .2, .2);
  128.     NXUnionRect( &tmpRect, clip);    
  129.     NXSetRect( &tmpRect, pyramid[2][0] - .1, pyramid[2][1] - .1, .2, .2);
  130.     NXUnionRect( &tmpRect, clip);    
  131.     NXSetRect( &tmpRect, pyramid[3][0] - .1, pyramid[3][1] - .1, .2, .2);
  132.     NXUnionRect( &tmpRect, clip);    
  133.     
  134.     return( self);
  135.     }
  136.  
  137.  
  138. @end
  139.  
  140. #ifdef    _LOG
  141. /*
  142.  *    $Log:    Pyramid.m,v $
  143. Revision 1.1  93/09/15  12:35:12  pkron
  144. Created.
  145.  
  146.  */
  147. #endif        
  148.